/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kurs;

/**
 *
 * @author Matrix
 */
public class DynQueue implements Queue {

    private BaseNode first, last;

    private int size;

    public DynQueue() {
        first = last = new BaseNode();
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return (first.link == null);
    }

    public BaseNode first() throws EmptyException {
        if (isEmpty()) throw new EmptyException("Queue is empty, try to enqueue something");
        return first;
    }

    public BaseNode last() throws EmptyException {
        if (isEmpty()) throw new EmptyException("Queue is empty, try to enqueue something");
        return last;
    }

    public void enqueue(BaseNode node) {
        node.link = null;
        last.link = node;
        last = node;
        size += 1;

    }

    public BaseNode dequeue() throws EmptyException {
        if (isEmpty()) throw new EmptyException("Queue is empty, try to enqueue something");
        BaseNode temp = first.link;
        first = first.link;
        size -= 1;
        return temp; 
    }

    public String printState() throws EmptyException {
        if (isEmpty()) throw new EmptyException("Queue is empty");
        BaseNode temp = first.link;
        String output = new String();
        do {
            //System.out.print(temp.info + " ");
            output += temp.getInfo() + " ";
            temp = temp.link;
        } while (temp != null);
        //System.out.println("");
        return output;
    }

    public BaseNode find(String info) throws EmptyException, NotFoundException {
        if (isEmpty()) throw new EmptyException("Queue is empty, try to enqueue something");
        BaseNode temp = first.link;
        while ((temp != null)&&(!temp.getInfo().equals(info))) {
            temp = temp.link;
        }
        if (temp == null) throw new NotFoundException("Element not found");
        return temp;
    }

}
